//添加第一级菜单
function addOneLevel(){
	
	//生成第一级菜单
	addoption("1",".select1");
	
	$('.select_type select').bind({
		change: function() {
			
			var pid = $(this).val();
			var select_level = $(this).attr("name");
			
			if(select_level=="select1"){
				pid  = $(".select1").val();
				var option  = "";
				$(".select2").html(option)
				$(".select3").html(option)
				$(".select4").html(option)
				addoption(pid,".select2");
				
			}
				
			if(select_level=="select2"){
				pid  = $(".select2").val();
				var option  = "";
				$(".select3").html(option)
				$(".select4").html(option)
				addoption(pid,".select3");
			}
			
			if(select_level=="select3"){
				var option  = "";
				$(".select4").html(option)
				pid  = $(".select3").val();
				addoption(pid,".select4");
			}
			
	    }
	});
	
	
}



//生成1-4级菜单的选项
function addoption(pid,select_level){
		var sjson = DucumentFunction.getProductType(pid);
		
		if(sjson!=null&&sjson.length>0){
			var json = strToJson(sjson);
			var option  = "";
			$(select_level).html(option);
			for(var i=0; i<json.length; i++)  
			  {  	
					 option =option+ " <option id=\"op"+json[i].id+"\" value=\""+json[i].id+"\">"+json[i].name+"</option>";
				
			  };
			$(select_level).html(option);
			//如果是加载第一级选项
			if(select_level==".select1"&&option!=null&&option.length>0){
				pid  = $(".select1").val();
				
				addoption(pid,".select2");
			}
			
			if(select_level==".select2"&&option!=null&&option.length>0){
				pid  = $(".select2").val();
				addoption(pid,".select3");
			}
			
			if(select_level==".select3"&&option!=null&&option.length>0){
				pid  = $(".select3").val();
				addoption(pid,".select4");
			}
			
		} 
}



//1，传入当前产品的所有子类和父类 2，选择选项的熟悉"#xxx" ".xXX" 3，pid
function setop(pr_json,selectlv,pid){

	 var  id = 1;
	 //根据Pid查询所有子类
	 var sjson = DucumentFunction.getProductType(pid);

	 var json = strToJson(sjson);
	 var p_json = strToJson(pr_json);

	 	//初始化
		var option  = "";
		$(selectlv).html(option);
		for(var i=0; i<json.length; i++)  
		  {  	
			var selected = "";
			for(var ii=0; ii<p_json.length; ii++)  
			  {  
					if(p_json[ii].pid == pid){
						selected = p_json[ii].id
					
					}
				
			  }

		
				//筛选出一级菜单的选项
				if(json[i].id != selected){
				 	option =option+ " <option id=\"op"+json[i].id+"\" value=\""+json[i].id+"\">"+json[i].name+"</option>";
				}
				else{	
				
					option =option+ " <option  selected = \"selected\"  id=\"op"+json[i].id+"\" value=\""+json[i].id+"\">"+json[i].name+"</option>";
				}
			
		  };
		 
		  
		  $(selectlv).html(option);
		  
		  
		  //递归
		  if(selectlv==".select1"&&option!=null&&option.length>0){
				pid  = $(".select1").val();	
			
				 setop(pr_json,".select2",pid);
			}
			
			if(selectlv==".select2"&&option!=null&&option.length>0){
				pid  = $(".select2").val();
				
				 setop(pr_json,".select3",pid);
			}
			
			if(selectlv==".select3"&&option!=null&&option.length>0){
				
				pid  = $(".select3").val();
				setop(pr_json,".select4",pid);
			}
	 		
	
}